/**
 * @File AccountMasterDaoImpl.java
 * @author Dheeraj Singh
 * @Version 1.0
 * @Date March 25, 2013
 * 
 * Copyright (c) 2011 EdRP, Dayalbagh Educational Institute.
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this  list of conditions and the following disclaimer.
 *
 * Redistribution in binary form must reproducuce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the
 * distribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL ETRG OR ITS CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Contributors: Members of EdRP, Dayalbagh Educational Institute
 */
package in.ac.dei.edrp.purchase.accountMaster;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.stereotype.Component;

import com.ibatis.sqlmap.client.SqlMapClient;

@Component
@Qualifier("AccountMasterDaoImpl")
public class AccountMasterDaoImpl extends SqlMapClientDaoSupport implements AccountMasterDao {

	private static Logger logObj = Logger.getLogger(AccountMasterDaoImpl.class);
	
	AccountMasterDaoImpl(){}
	
	@Autowired
	public AccountMasterDaoImpl(SqlMapClient sqlMapClient){
		setSqlMapClient(sqlMapClient);
	}
	
	public List<AccountMasterInfoBean> getCompanyDetails(AccountMasterInfoBean input) {
		List<AccountMasterInfoBean> companyList = null;
		try{
			companyList = getSqlMapClientTemplate().queryForList("accountMaster.selectCompanyDetails", input);
		}catch(Exception ex){
			logObj.info("Exception in getCompanyDetails method");
			logObj.error(ex);
		}
		return companyList;
	}
	
	public List<AccountMasterInfoBean> getAccountChartDetails(AccountMasterInfoBean input) {
		List<AccountMasterInfoBean> chartList = null;
		try{
			chartList = getSqlMapClientTemplate().queryForList("accountMaster.selectAccountChartDetails", input);
		}catch(Exception ex){
			logObj.info("Exception in getAccountChartDetails method");
			logObj.error(ex);
		}
		return chartList;
	}
	
	public List<AccountMasterInfoBean> getAccountMasterDetails(AccountMasterInfoBean input) {
		List<AccountMasterInfoBean> accountsList = null;
		try{
			accountsList = getSqlMapClientTemplate().queryForList("accountMaster.selectAccountMasterDetails", input);
		}catch(Exception ex){
			logObj.info("Exception in getAccountMasterDetails method");
			logObj.error(ex);
		}
		return accountsList;
	}
	
	public List<AccountMasterInfoBean> getAccountTypeDetails(AccountMasterInfoBean input) {
		List<AccountMasterInfoBean> accountTypeList = null;
		try{
			accountTypeList = getSqlMapClientTemplate().queryForList("accountMaster.selectAccountTypeDetails", input);
		}catch(Exception ex){
			logObj.info("Exception in getAccountTypeDetails method");
			logObj.error(ex);
		}
		return accountTypeList;
	}

	public String createNewAccount(AccountMasterInfoBean input) {
		String accountCode = "";
		try{
			accountCode = input.getParentAccountCode() + "." + input.getAccountCode();
			List<AccountMasterInfoBean> obj = new ArrayList<AccountMasterInfoBean>();
			input.setAccountCode(accountCode);
			obj = getSqlMapClientTemplate().queryForList("accountMaster.checkDuplicateAccountCode", input);
			if(obj.size() > 0){
				accountCode = "CodeExists";
				return accountCode;
			}else{
				getSqlMapClientTemplate().insert("accountMaster.insertAccountMasterDetails", input);
			}
		}catch(Exception ex){
			
			logObj.info("Exception in createNewAccount method");
			logObj.error(ex);
		}
		return accountCode;
	}

	public AccountMasterInfoBean getAccountDetailsForEditing(AccountMasterInfoBean input) {
		AccountMasterInfoBean output = new AccountMasterInfoBean();
		try{
			output = (AccountMasterInfoBean) getSqlMapClientTemplate().queryForObject("accountMaster.selectAccountDetailsForEditing", input);
		}catch(Exception ex){
			logObj.info("Exception in getAccountDetailsForEditing method");
			logObj.error(ex);
		}
		return output;
	}

	public boolean updateAccountProperties(AccountMasterInfoBean input) {
		boolean flag = false;
		try{
			getSqlMapClientTemplate().update("accountMaster.updateAccountDetails", input);
			flag = true;
		}catch(Exception ex){
			System.out.println(ex);
			logObj.info("Exception in updateAccountProperties method");
			logObj.error(ex);
		}
		return flag;
	}

	public String deleteAccount(AccountMasterInfoBean input) {
		String message = "";
		AccountMasterInfoBean checkBean = new AccountMasterInfoBean();
		checkBean.setAccountCode(input.getAccountCode());
		checkBean.setCompanyCode(input.getCompanyCode());
		checkBean.setChartCode(input.getChartCode());
		checkBean.setUniversityCode(input.getUniversityCode());
		try{
			List<AccountMasterInfoBean> checkList = new ArrayList<AccountMasterInfoBean>();
			checkList = getSqlMapClientTemplate().queryForList("accountMaster.checkChildAccounts", checkBean);
			if(checkList.size() > 0){
				message = "HasChildTrue";
				return message;
			}else{
				getSqlMapClientTemplate().delete("accountMaster.deleteAccount", input);
				message = "Deleted";
			}
		}catch(Exception ex){
			System.out.println(ex);
			logObj.info("Exception in deleteAccount method");
			logObj.error(ex);
		}
		return message;
	}

	public String createNewGroupAccount(AccountMasterInfoBean input) {
		String accountCode = "";
		try{
			AccountMasterInfoBean beanObj = new AccountMasterInfoBean();
			if(input.getPrimaryAccount().equalsIgnoreCase("Y")){
				input.setAccountDepth("1");
				beanObj = (AccountMasterInfoBean) getSqlMapClientTemplate().queryForObject("accountMaster.getMaxGroupAccountCode", input);
				String maxId = beanObj.getMaxGroupAccountCode();
				if(maxId == null){
					accountCode = input.getAccountTypeCode() + ".01" ;
					input.setAccountCode(accountCode);
				}else{
					String seqNum = maxId.substring(maxId.length()-2, maxId.length());
					int newSeqNum = Integer.parseInt(seqNum);
					newSeqNum++;
					String val = String.format("%02d", newSeqNum);
					accountCode = input.getAccountTypeCode() + "." + val;
					input.setAccountCode(accountCode);
				}
				getSqlMapClientTemplate().insert("accountMaster.insertGroupAccount", input);
			}else{
				beanObj = (AccountMasterInfoBean) getSqlMapClientTemplate().queryForObject("accountMaster.selectAccountDepth", input);
				int childCount = beanObj.getChildCount();
				childCount++;
				input.setChildCount(childCount);
				String depth = beanObj.getAccountDepth();
				int level = Integer.parseInt(depth);
				level++;
				input.setAccountDepth(String.valueOf(level));
				beanObj = (AccountMasterInfoBean) getSqlMapClientTemplate().queryForObject("accountMaster.getMaxGroupAccountCode", input);
				String maxId = beanObj.getMaxGroupAccountCode();
				if(maxId == null){
					accountCode = input.getParentAccountCode() + ".01" ;
					input.setAccountCode(accountCode);
				}else{
					String seqNum = maxId.substring(maxId.length()-2, maxId.length());
					int newSeqNum = Integer.parseInt(seqNum);
					newSeqNum++;
					String val = String.format("%02d", newSeqNum);
					accountCode = input.getParentAccountCode() + "." + val;
					input.setAccountCode(accountCode);
				}
				getSqlMapClientTemplate().insert("accountMaster.insertGroupAccount", input);
				getSqlMapClientTemplate().update("accountMaster.updateChildCount", input);
			}
		}catch(Exception ex){
			System.out.println("Exception : " + ex);
		}
		return accountCode;
	}

	public List<AccountMasterInfoBean> getGroupAccountDetails(
			AccountMasterInfoBean input) {
		List<AccountMasterInfoBean> accountsList = null;
		try{
			accountsList = getSqlMapClientTemplate().queryForList("accountMaster.selectGroupAccounts", input);
		}catch(Exception ex){
			logObj.info("Exception in getAccountMasterDetails method");
			logObj.error(ex);
		}
		return accountsList;
	}

	public AccountMasterInfoBean getGroupAccountDetailsForEditing(
			AccountMasterInfoBean input) {
		AccountMasterInfoBean output = new AccountMasterInfoBean();
		try{
			output = (AccountMasterInfoBean) getSqlMapClientTemplate().queryForObject("accountMaster.selectGroupAccountDetailsForEditing", input);
		}catch(Exception ex){
			logObj.info("Exception in getAccountDetailsForEditing method");
			logObj.error(ex);
		}
		return output;
	}

	public boolean updateGroupAccountProperties(AccountMasterInfoBean input) {
		boolean flag = false;
		try{
			getSqlMapClientTemplate().update("accountMaster.updateGroupAccountDetails", input);
			flag = true;
		}catch(Exception ex){
			System.out.println(ex);
			logObj.info("Exception in updateAccountProperties method");
			logObj.error(ex);
		}
		return flag;
	}

	public String deleteGroupAccount(AccountMasterInfoBean input) {
		String message = "";
		AccountMasterInfoBean checkBean = new AccountMasterInfoBean();
		checkBean.setAccountCode(input.getAccountCode());
		checkBean.setCompanyCode(input.getCompanyCode());
		checkBean.setChartCode(input.getChartCode());
		checkBean.setUniversityCode(input.getUniversityCode());
		try{
			List<AccountMasterInfoBean> checkList = new ArrayList<AccountMasterInfoBean>();
			checkList = getSqlMapClientTemplate().queryForList("accountMaster.checkGroupChildAccounts", checkBean);
			if(checkList.size() > 0){
				message = "HasChildTrue";
				return message;
			}else{
				checkList = getSqlMapClientTemplate().queryForList("accountMaster.checkChildAccounts", checkBean);
				if(checkList.size() > 0){
					message = "HasChildTrue";
					return message;
				}
			}
			if(checkList.size() == 0){
				getSqlMapClientTemplate().delete("accountMaster.deleteGroupAccount", input);
				getSqlMapClientTemplate().update("accountMaster.updateChildCountAfterDeletion", input);
				message = "Deleted";
			}
		}catch(Exception ex){
			System.out.println(ex);
			logObj.info("Exception in deleteAccount method");
			logObj.error(ex);
		}
		return message;
	}
}
